package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.enums.SeeRecordEnums;
import one.stand.enums.StateEnum;
import one.stand.model.*;
import one.stand.request.AppointmentRequest;
import one.stand.request.ProductsRequest;
import one.stand.request.ProductsTypeRequest;
import one.stand.response.AppointmentResponse;
import one.stand.response.ProductsResponse;
import one.stand.response.ProductsTypeResponse;
import one.stand.service.BaseService;
import one.stand.service.ProductsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@Transactional
public class ProductsServiceImpl extends BaseService implements ProductsService {

    @Override
    public ResultModel<Map<String, Object>> getProductsCount(AppointmentRequest request) {
        if (request == null || request.getCompanyId() == null) {
            return ResultModel.checkParamFail("companyId: 公司ID为空");
        }
        Map<String, Object> map = appointmentMapper.getProductsCount(request.getCompanyId());
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<List<ProductsTypeResponse>> selectProductsTypeList(ProductsTypeRequest request) {
        if (request == null || request.getCompanyId() == null) {
            return ResultModel.checkParamFail("companyId: 公司ID为空");
        }
        List<ProductsTypeModel> list = productsTypeMapper.selectProductsTypeList(request.getCompanyId());
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        List<ProductsTypeResponse> responseList = new ArrayList<>();
        list.forEach(obj -> {
            {
                ProductsTypeResponse response = new ProductsTypeResponse();
                BeanUtils.copyProperties(obj, response);
                responseList.add(response);
            }
        });
        return ResultModel.success(responseList);
    }

    @Override
    public ResultModel<Map<String, Object>> selectProductsTypePage(ProductsTypeRequest request) {
        if (request == null || request.getCompanyId() == null) {
            return ResultModel.checkParamFail("companyId: 公司ID为空");
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        ProductsTypeModel model = new ProductsTypeModel();
        BeanUtils.copyProperties(request, model);
        model.setCompanyId(request.getCompanyId());
        List<ProductsTypeModel> list = productsTypeMapper.select(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        List<ProductsTypeResponse> responseList = new ArrayList<>();
        list.forEach(obj -> {
            {
                ProductsTypeResponse response = new ProductsTypeResponse();
                BeanUtils.copyProperties(obj, response);
                responseList.add(response);
            }
        });
        PageInfo<ProductsTypeResponse> pageInfo = new PageInfo<>(responseList);
        Map<String, Object> map = new HashMap<>(1);
        map.put("pageInfo", pageInfo);
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<List<ProductsTypeResponse>> selectProductsType(ProductsTypeRequest request) {
        if (request == null || request.getCompanyId() == null) {
            return ResultModel.checkParamFail("companyId: 公司ID为空");
        }
        ProductsTypeModel model = new ProductsTypeModel();
        BeanUtils.copyProperties(request, model);
        model.setCompanyId(request.getCompanyId());
        List<ProductsTypeModel> list = productsTypeMapper.selectList(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        List<ProductsTypeResponse> responseList = new ArrayList<>();
        list.forEach(obj -> {
            {
                ProductsTypeResponse response = new ProductsTypeResponse();
                BeanUtils.copyProperties(obj, response);
                responseList.add(response);
            }
        });
        return ResultModel.success(responseList);
    }

    @Override
    public ResultModel<Map<String, Object>> selectProductsPage(ProductsRequest request) {
        if (request == null || request.getCompanyId() == null) {
            return ResultModel.checkParamFail("companyId: 公司ID为空");
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        ProductsModel model = new ProductsModel();
        BeanUtils.copyProperties(request, model);
        model.setCompanyId(request.getCompanyId());
        List<ProductsModel> list = productsMapper.selectList(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        List<ProductsResponse> responseList = new ArrayList<>();
        list.forEach(obj -> {
            {
                ProductsResponse response = new ProductsResponse();
                BeanUtils.copyProperties(obj, response);
                responseList.add(response);
            }
        });
        PageInfo<ProductsResponse> pageInfo = new PageInfo<>(responseList);
        Map<String, Object> map = new HashMap<>(1);
        map.put("pageInfo", pageInfo);
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<ProductsResponse> getProductsInfo(ProductsRequest request) {
        if (request == null || request.getProductsId() == null) {
            return ResultModel.checkParamFail("productsId: 商品ID为空");
        }
        ProductsModel model = new ProductsModel();
        model.setProductsId(request.getProductsId());
        List<ProductsModel> list = productsMapper.selectList(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        ProductsResponse response = new ProductsResponse();
        BeanUtils.copyProperties(list.get(0), response);
        return ResultModel.success(response);
    }


    @Override
    public ResultModel<Map<String, Object>> selectAppointmentPage(AppointmentRequest request) {
        if (request == null || request.getId() == null) {
            return ResultModel.checkParamFail("id: ID为空");
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        AppointmentModel model = new AppointmentModel();
        BeanUtils.copyProperties(request, model);
        model.setUserId(request.getId().intValue());
        List<AppointmentModel> list = appointmentMapper.selectList(model);
        if (list == null || list.isEmpty()) {
            return ResultModel.noData();
        }

        if (request.getId() != null && list.get(0).getCompanyId() != request.getCId().intValue()) {
            //添加查看记录
            //addSeeRecord(request.getId().intValue(), request.getCardId(), list.get(0).getCompanyId(), SeeRecordEnums.APPOINTMENT.getCode(), null, 1);
        }

        PageInfo<AppointmentModel> pageInfo = new PageInfo<>(list);
        pageInfo.getList().forEach(obj -> {
            {
                obj.setNickeName(StringUtils.isBlank(obj.getNickeName()) ? "" : new String(java.util.Base64.getDecoder().decode(obj.getNickeName())));
            }
        });
        Map<String, Object> map = new HashMap<>(1);
        map.put("pageInfo", JSONObject.parse(JSON.toJSONString(pageInfo)));
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<AppointmentResponse> getAppointmentInfo(AppointmentRequest request) {
        if (request == null || request.getAppointmentId() == null) {
            return ResultModel.checkParamFail("appointmentId: 预约ID为空");
        }
        AppointmentModel model = new AppointmentModel();
        model.setAppointmentId(request.getAppointmentId());
        List<AppointmentModel> list = appointmentMapper.selectList(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }

        if (request.getCId() == null || list.get(0).getCompanyId() != request.getCId().intValue()) {
            //添加查看记录
            addSeeRecord(request.getId().intValue(), request.getCardId(), request.getCompanyId(), SeeRecordEnums.APPOINTMENT.getCode(), list.get(0).getAppointmentId(), 0);
        }

        AppointmentResponse response = new AppointmentResponse();
        BeanUtils.copyProperties(list.get(0), response);
        response.setNickeName(StringUtils.isBlank(list.get(0).getNickeName()) ? "" : new String(java.util.Base64.getDecoder().decode(list.get(0).getNickeName())));
        return ResultModel.success(response);
    }

    @Override
    public ResultModel<Boolean> insertAppointment(AppointmentRequest request) {
        if (request == null || request.getCompanyId() == null) {
            return ResultModel.checkParamFail("companyId: 公司ID为空");
        }
        if (request.getProductsId() == null) {
            return ResultModel.fail("请选择预约产品~！");
        }
        if (request.getName() == null) {
            return ResultModel.fail("请输入姓名~！");
        }
        if (request.getPhone() == null) {
            return ResultModel.fail("请输入电话~！");
        }
        if (request.getServiceType() == null) {
            return ResultModel.fail("请输入服务类型~！");
        }
        if (request.getStartTimes() == null) {
            return ResultModel.fail("请输入预约开始时间~！");
        }
        if (request.getEndTimes() == null) {
            return ResultModel.fail("请输入预约结束时间~！");
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            AppointmentModel model = new AppointmentModel();
            model.setCompanyId(request.getCompanyId());
            model.setName(request.getName());
            model.setPhone(request.getPhone());
            model.setServiceType(request.getServiceType());
            model.setStartTime(formatter.parse(request.getStartTimes() + ":00"));
            model.setEndTime(formatter.parse(request.getEndTimes() + ":00"));
            model.setUserId(request.getId().intValue());
            model.setCreateTime(currentDate());
            model.setIsDel(0);
            model.setState(StateEnum.STATE_APPLY.getCode());
            model.setRemark(request.getRemark());
            model.setProductsId(request.getProductsId());
            appointmentMapper.insertSelective(model);
            return ResultModel.success(true);
        } catch (Exception e) {
            log.error("新增预约[异常]", e);
            return ResultModel.fail("新增预约[异常]：" + e.getMessage());
        }
    }

    @Override
    public ResultModel<Boolean> updAppointmentState(AppointmentRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        } else if (request.getAppointmentId() == null) {
            return ResultModel.checkParamFail("appointmentId: 预约ID为空");
        } else if (request.getState() == null) {
            return ResultModel.checkParamFail("state: 状态为空");
        }
        AppointmentModel model = new AppointmentModel();
        model.setAppointmentId(request.getAppointmentId());
        model.setState(request.getState());
        model.setApplyRemark(request.getApplyRemark());
        if (3 == request.getState()) {
            model.setCompletionTime(currentDate());
        }
        if (4 == request.getState()) {
            model.setCancellationTime(currentDate());
        }
        appointmentMapper.updateByPrimaryKeySelective(model);
        return ResultModel.success(true);
    }
}
